//
//  RMDateModel.m
//  RMNote
//
//  Created by lxf on 16/4/15.
//  Copyright © 2016年 lxf. All rights reserved.
//

#import "RMDateModel.h"
#import "RMTimeHandle.h"
#import "RMBaseModel.h"
#import "RMDateBaseManager.h"

@interface RMDateModel ()
@property (nonatomic, strong) NSMutableDictionary *yearDic;
@property (nonatomic, strong) NSMutableDictionary *monthDic;
@end

@implementation RMDateModel

- (instancetype)initWithDateArr:(NSArray *)dateArr{
    if (self = [super init]) {
        self.dateArr = dateArr;
        
        [self getMonthsAndTimeArr];

    }
    return self;
}

- (void)setDateArr:(NSArray *)dateArr{
    _dateArr = [self sortWithDateArr:dateArr];
    NSDictionary *yearDic = [[RMTimeHandle shareInstance] dataDrawUntilDayWithDateArr:dateArr];
    self.yearDic = yearDic.mutableCopy;
    
    self.years = yearDic.allKeys.mutableCopy;
    self.years = [self sortWithArr:self.years];
    
   [self getYMAndYMDWithDateArr:dateArr];
    
//    _dateArr = dateArr;
}

- (void)getYMAndYMDWithDateArr:(NSArray *)arr{

    NSMutableArray *dateTmpArr = [self sortWithDateArr:arr].mutableCopy;
//    NSString *dateStr = [NSString stringWithFormat:@"%@"]
    for (NSDate *date in dateTmpArr) {
        long int ym = date.year.intValue * pow(10, 10) + date.month.intValue * pow(10, 8);
        [self.YMArr addObject:[NSString stringWithFormat:@"%ld",ym]];
        long int ymd = date.year.intValue * pow(10, 10) + date.month.intValue * pow(10, 8) + date.day.intValue*pow(10, 6);
        [self.YMDArr addObject:[NSString stringWithFormat:@"%ld",ymd]];
    }
}


- (NSArray *)monthsForYear:(NSString *)year{
    self.monthDic = [self.yearDic objectForKey:year];
    NSMutableArray *monthArr = self.monthDic.allKeys.mutableCopy;
    monthArr = [self sortWithArr:monthArr].mutableCopy;
    return monthArr;
}

- (NSArray *)fullDaysForYear:(NSString *)year andMonth:(NSString *)month{
    self.monthDic = [self.yearDic objectForKey:year];
    NSMutableArray *dayArr = [self.monthDic objectForKey:month];
    dayArr = [self sortWithDateArr:dayArr].mutableCopy;
    return dayArr;
}

- (NSArray *)timesForYear:(NSString *)year andMonth:(NSString *)month andDay:(NSString *)day{
    NSArray *timesArr = [NSMutableArray array];
    
    return timesArr;
}


/**
 *  获取月的数组,获取月对应日的数组
 */
- (void)getMonthsAndTimeArr{
    for (NSString *year in self.years) {
        NSArray *months = [self monthsForYear:year];
        [self.monthArr addObjectsFromArray:months];
        //        [self.monthArr addObject:months];
        for (NSString *month in months) {
            
            NSString *month_year = [NSString stringWithFormat:@"%@-%@",month,year];
            [self.month_yearArr addObject:month_year];
            
            NSMutableArray *tmpDayArr = [[RMDateBaseManager defaultManager]selectNotebySQLStr:[NSString stringWithFormat:@"select RMModel from %@ where year = %@ and month = %@",kRMNoteTable,year,month]].mutableCopy;
            [tmpDayArr sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
                RMBaseModel *model1 = (RMBaseModel *)obj1;
                RMBaseModel *model2 = (RMBaseModel *)obj2;
                if ([model1.createDate compare:model2.createDate]==NSOrderedAscending) {
                    return NSOrderedDescending;
                }else if([model1.createDate compare:model2.createDate]==NSOrderedDescending){
                    return NSOrderedAscending;
                }else{
                    return NSOrderedSame;
                }
            }];
            [self.month_DayArr addObject:tmpDayArr];
            
        }
        
    }
    
}




- (NSArray *)sortWithArr:(NSArray *)arr{
    NSArray *arr1 =[arr sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        long lg1 = [(NSString *)obj1 intValue];
        long lg2 = [(NSString *)obj2 intValue];
        if (lg1 > lg2) {
            return NSOrderedAscending;
        }else if(lg1 < lg2){
            return NSOrderedDescending;
        }else{
            return NSOrderedSame;
        }
    }];
    return arr1;
}

- (NSArray *)sortWithDateArr:(NSArray *)arr{
    NSArray *arr1 =[arr sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        NSDate *date1 = (NSDate *)obj1;
        NSDate *date2 = (NSDate *)obj2;
        if ([date1 compare:date2] == NSOrderedAscending) {
            return NSOrderedDescending;
        }else if([date1 compare:date2] == NSOrderedDescending){
            return NSOrderedAscending;
        }else{
            return NSOrderedSame;
        }
    }];
    return arr1;
}


#pragma mark - 懒加载
- (NSMutableArray *)monthArr{
    if (!_monthArr) {
        _monthArr = [NSMutableArray array];
    }
    return _monthArr;
}

- (NSMutableArray *)month_DayArr{
    if (!_month_DayArr) {
        _month_DayArr = [NSMutableArray array];
    }
    return _month_DayArr;
}

- (NSMutableArray *)month_yearArr{
    if (!_month_yearArr) {
        _month_yearArr = [NSMutableArray array];
    }
    return _month_yearArr;
}

- (NSMutableArray *)YMArr{
    if (!_YMArr) {
        _YMArr = [NSMutableArray array];
    }
    return _YMArr;
}

- (NSMutableArray *)YMDArr{
    if (!_YMDArr) {
        _YMDArr = [NSMutableArray array];
    }
    return _YMDArr;
}


@end
